package com.humanres.activity;


import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.ciko.code.base.BaseActicity;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.Req.QryKnowledgeDocumentListEty;
import com.ciko.code.http.Req.QryKnowledgeDocumentListReq;
import com.ciko.code.utils.NetWorkUtils;
import com.ciko.code.utils.Util;
import com.ciko.code.views.pullrefresh.PullToRefreshBase;
import com.ciko.code.views.pullrefresh.PullToRefreshListView;
import com.mywork.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 12154 on 2015/11/1.
 */
public class ResOtherActivity extends BaseActicity {
    private ListView otherList;
    private PullToRefreshListView pullToRefreshListView;
    private LinearLayout llNone;
    private TextView tvTip;
    private Context context;
    private int length = 20;
    private int orderNum = 0;
    private MyListAdapter myListAdapter;
    private List<QryKnowledgeDocumentListEty> listEtyLists = new ArrayList<>();
    private String title = "";
    private String isDocumentFile = "n";

    @Override
    protected void initTopBar() {
        if (getIntent().getExtras().containsKey("title")) {
            title = getIntent().getStringExtra("title");
        }
        if (title.equals("公文传阅")) {
            isDocumentFile = "y";
        } else if (title.equals("知识文库")) {
            isDocumentFile = "n";
        }
        setTitle(title);
        enableNavLeftImg();
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_res_other);
        context = this;
        if (!NetWorkUtils.isConnected(context)) {
            Util.showToast(context, "网络错误，请检查网络");
            return;
        }
        getOther();
    }

    private void getOther() {
        doRequestHttp(new QryKnowledgeDocumentListReq(length, orderNum, isDocumentFile));
    }

    @Override
    protected void initView() {
        pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.res_other_list);
        llNone = (LinearLayout) findViewById(R.id.ll_none_things);
        tvTip = (TextView) findViewById(R.id.tv_tip);
        otherList = pullToRefreshListView.getRefreshableView();
    }

    @Override
    protected void initAdapter() {
        myListAdapter = new MyListAdapter();
        otherList.setAdapter(myListAdapter);
        pullToRefreshListView.setPullLoadEnabled(true);
        pullToRefreshListView.setScrollLoadEnabled(true);
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                orderNum = 0;
                doRequestHttp(new QryKnowledgeDocumentListReq(length, orderNum, isDocumentFile));
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                doRequestHttp(new QryKnowledgeDocumentListReq(length, orderNum, isDocumentFile));
            }
        });
    }

    @Override
    protected void initDates() {

    }

    @Override
    protected void initListener() {
        otherList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(context, CloudReadActivity.class);
                intent.putExtra("QryKnowledgeDocumentListEty", listEtyLists.get(i));
                intent.putExtra("isDocumentFile", isDocumentFile);
                intent.putExtra("item", i);
                startActivityForResult(intent, 0);
            }
        });
    }


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {//resultCode为回传的标记，我在B中回传的是RESULT_OK
            case RESULT_OK:
                try {
                    int i = data.getIntExtra("item", -1);
                    listEtyLists.get(i).setIsRead("y");
                    myListAdapter.notifyDataSetChanged();
                }catch (Exception ignored) {

                }
                break;
        }
    }

    private class MyListAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return listEtyLists.size();
        }

        @Override
        public Object getItem(int i) {
            return listEtyLists.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if (view == null) {
                holder = new ViewHolder();
                view = View.inflate(context, R.layout.other_res_item, null);
                holder.tvName = (TextView) view.findViewById(R.id.res_other_item_tv);
                holder.img_document_tag = (ImageView) view.findViewById(R.id.img_document_tag);
                holder.iv_res_oval = (ImageView) view.findViewById(R.id.iv_res_oval);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            if (!TextUtils.isEmpty(listEtyLists.get(i).getIsImportant())) {
                if (listEtyLists.get(i).getIsImportant().equals("y")) {
                    holder.img_document_tag.setVisibility(View.VISIBLE);
                } else {
                    holder.img_document_tag.setVisibility(View.INVISIBLE);
                }
            }
            holder.tvName.setText(listEtyLists.get(i).getTitle());

            //为未读公文添加标记
            if ("公文传阅".equals(title) && "n".equals(listEtyLists.get(i).getIsRead())) {
                holder.iv_res_oval.setVisibility(View.VISIBLE);
            } else {
                holder.iv_res_oval.setVisibility(View.GONE);
            }
            return view;
        }
    }

    private class ViewHolder {
        private TextView tvName;
        private ImageView img_document_tag;
        private ImageView iv_res_oval;
    }

    @Override
    public void onResponseSuccess(BaseResponse<?> response, String tag) {
        super.onResponseSuccess(response, tag);
        Log.i("info========1", tag);
        switch (tag) {
            case "QryKnowledgeDocumentListReq":
                List<QryKnowledgeDocumentListEty> listEties = (List<QryKnowledgeDocumentListEty>) response.getReturnObject();
                if (orderNum == 0) {
                    pullToRefreshListView.onPullDownRefreshComplete();
                    listEtyLists.clear();
                    if (listEties.size() == 0) {
                        llNone.setVisibility(View.VISIBLE);
                        pullToRefreshListView.setVisibility(View.GONE);
                    } else {
                        llNone.setVisibility(View.GONE);
                        pullToRefreshListView.setVisibility(View.VISIBLE);
                    }
                } else {
                    pullToRefreshListView.onPullUpRefreshComplete();
                }
                listEtyLists.addAll(listEties);
                orderNum = listEtyLists.get(listEtyLists.size() - 1).getOrderNum();
                myListAdapter.notifyDataSetChanged();
//                Util.showToast(this, "");
                break;
        }
    }

    @Override
    public void onResponseFail(String tag, int resultCode) {
        super.onResponseFail(tag, resultCode);
        Log.i("info========2", tag);
        if (orderNum == 0) {
            pullToRefreshListView.onPullDownRefreshComplete();
        } else {
            pullToRefreshListView.onPullUpRefreshComplete();
        }
    }
}
